#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->diode_test();

}

MainWindow::~MainWindow()
{
    delete ui;
}

//void MainWindow::init_diode()
//{
//    QwtPlot *p = new QwtPlot();


//    p->setTitle("二极管伏安特性曲线");
//    p->setAxisTitle(QwtPlot::xBottom, "二极管两端电压");
//    p->setAxisTitle(QwtPlot::yLeft, "流过二极管的电流");
//    //p->setParent(ui->centralwidget);
//    p->resize(800, 500);
//    p->show();
//    QwtPlotGrid *grid = new QwtPlotGrid;
//    grid->enableX( true );//设置网格线
//    grid->enableY( true );
//    grid->setMajorPen( Qt::black, 0, Qt::DotLine );
//    grid->attach( p );

//    QwtPlotCurve *curve = new QwtPlotCurve("curve");

//    curve->setPen(Qt::blue,2);//设置曲线颜色 粗细
//    curve->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化

//    //QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
//    //QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 6, 6) );//设置样本点的颜色、大小
//    //curve->setSymbol( symbol );//添加样本点形状

//    QPolygonF points1;//输入节点数据QPointF(x,y)


//    diode d = diode();
//    d.setV_ON(0.7);
//    d.setV_TH(0.5);
//    d.setV_BR(-150);
//    d.setI_S(10e-14);
//    for(float V_D = -160.0; V_D <= 0; V_D += 5){    //分段函数

//        if(V_D <= d.getV_BR()){
//            d.setI_D(-2);
//            //points1<<QPointF(d.getV_D(),d.getI_D());
//        }else if (V_D > d.getV_BR()){
//            d.setI_D(d.getI_S());
//            //points1<<QPointF(d.getV_D(),d.getI_D());
//        }
//        qDebug()<<d.getI_D();
//    }
//    for(float V_D = 0.0; V_D <= 0.8; V_D += 0.01){
//        d.setV_D(V_D);
//        d.setI_D(d.calI_D(d.getI_S(), d.getV_D()));
//        points1<<QPointF(d.getV_D(),d.getI_D());
//        qDebug()<<d.getI_D();
//    }
//    curve->setSamples(points1);
//    curve->attach( p );
//}

void MainWindow::diode_test()
{
     QwtPlot *p = new QwtPlot();
     p->resize(800, 500);
     p->show();
     QwtPlotGrid *grid = new QwtPlotGrid;
     grid->enableX( true );//设置网格线
     grid->enableY( true );
     grid->setMajorPen( Qt::black, 0, Qt::DotLine );
     grid->attach( p );
     QwtPlotCurve *curve = new QwtPlotCurve("curve");
     curve->setPen(Qt::blue,2);//设置曲线颜色 粗细
     curve->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化
     QPolygonF points1;
     diode d = diode();
     d.setV_ON(0.7);
     d.setV_TH(0.5);
     d.setV_BR(-150);
     d.setI_S(10e-14);
     for(float t = 0; t < 3; t += 0.0001){
         float y = 2* sin(5*t);
//         points1<<QPointF(t, y);
         if(y>=0.7){
             d.setV_output(y);
             points1<<QPointF(t, d.getV_output());
         }
//         }else if(y<=-0.7){
//             d.setV_output()
//         }
         //d.setV_input()
     }
     curve->setSamples(points1);
     curve->attach( p );

}
void MainWindow::on_pushButton_diode_clicked()
{
    //this->init_diode();
    diode *d = new diode();
    d->init_diode(0.7, 0.5, -150.0, 10e-14);


}
